#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@File    ：test5.py
@Author  ：Juff
@Date    ：2023/10/14 14:53 
'''

# wordcount 做词云图

#  pip install jieba
import pymysql
import jieba

# 分词
conn = pymysql.connect(host='master', user='root', password='123456', port=3306, db='ppCar')
cursor = conn.cursor()

select_sql = "select title from renren_used_car"
cursor.execute(select_sql)
data = cursor.fetchall()

wc_dict = {}

for title in data:
    seg_list = list(jieba.cut(title[0]))
    for seg in seg_list:
        if seg in wc_dict:
            wc_dict[seg] += 1
        else:
            wc_dict[seg] = 1
# print(data)
wc_dict.pop("-")
wc_dict.pop(" ")
wc_dict.pop("款")

cursor.execute("drop table if exists test_5")
cursor.execute("create table if not exists test_5 (word varchar(255),count int)")

wc_list = [(k, v) for k, v in wc_dict.items()]
print(wc_list)

# insert_sql = 'insert into test_5 values(%s,%s)' % wc_list[1]
# cursor.execute(insert_sql, wc_list)
# conn.commit()

try:
    insert_sql = 'insert into test_5 values(%s,%s)'
    cursor.executemany(insert_sql, wc_list)
except Exception as e:
    print(e)
    conn.rollback()
finally:
    conn.commit()

cursor.close()
conn.close()